<template>
  <div class="book">
    <div class="book_contain">
      <div class="book_left">
        <img src="@/assets/1.jpg" alt="" />
      </div>
      <div class="book_right">
        <h3>{{ book.title }}</h3>
        <p>{{ book.slogan }}</p>
        <p>
          <span>作者：{{ book.author }}</span>
          <span>出版社：{{ book.bookConcern }}</span>
          <span>出版日期：{{ book.publishDate }}</span>
        </p>
        <p>
          <span>{{ currency(factPrice(book.price, book.discount)) }}</span>
          <span> [{{ book.discount * 100 }}]折</span>
          <span>
            [定价<del>{{ book.price }}</del
            >]</span
          >
        </p>
      </div>
    </div>
    <div class="addCart">
      <AddSubtractButton @updateQuantity="handleUpdate" />
      <button @click="addCart(book)">加入购物车</button>
    </div>
    <BookTabComponent />
  </div>
</template>

<script>
import BookTabComponent from "@/components/BookTabComponent";
import AddSubtractButton from "@/components/AddSubtractButton";
import { mapMutations } from "vuex";
export default {
  name: "Book",
  components: {
    AddSubtractButton,
    BookTabComponent,
  },
  data() {
    return {
      book: {},
      quantity: 0,
    };
  },
  computed: {},

  methods: {
    ...mapMutations("cart", ["pushProductToCart"]),
    handleUpdate(value) {
      this.quantity = value;
    },
    addCart(book) {
      book.quantity = this.quantity;
      if (book.quantity == 0) {
        book.quantity = 1;
      }
      let cartItem = {
        ...book,
      };
      this.pushProductToCart(cartItem);
      this.$router.push("/cart");
    },
  },

  inject: ["factPrice", "currency"],

  created() {
    this.axios
      .get("/booksNews")
      .then((res) => {
        let itemBook = res.data.find(
          (item) => (item.id = this.$route.params.id)
        );
        this.book = itemBook;

      })
      .catch((err) => {
        console.log(err);
      });
  },
};
</script>
<style>
.book {
  width: 100%;
  height: 100%;
}

.book_contain {
  overflow: hidden;
}
.book_left {
  width: 20%;
  float: left;
}

.book_left img {
  width: 83%;
  height: auto;
  display: block;
}

.book_right {
  width: 80%;
  float: right;
  line-height: 50px;
  background: gray;
  padding-left: 10px;
  box-sizing: border-box;
}
.book_right h3 {
  font-weight: 700;
  font-size: 30px;
}

.addCart {
  overflow: hidden;
  width: 170px;
  margin: 0 auto;
  height: 100%;
}

.addCart button {
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
  float: right;
  margin-top: 10px;
}
</style>
